package com.qqtech.qquan.delivery.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.delivery.enums.DeliveryOrderDictTypeConstants;
import com.qqtech.qquan.delivery.model.DeliverySysParam;
import com.qqtech.qquan.delivery.model.DeliverySysParamQuery;
import com.qqtech.qquan.delivery.service.DeliverySysParamService;
import com.qqtech.qquan.user.model.UserAddress;
import com.qqtech.qquan.user.model.UserLoginVo;
import com.qqtech.qquan.user.service.UserAddressService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/delivery/sys/param")
public class DeliverySysParamController extends BaseWebController<DeliverySysParam, DeliverySysParamQuery> {
	@Resource
	private DeliverySysParamService deliverySysParamService;
	
	@Resource
	private UserAddressService userAddressService;
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/getDefaultValue")
	@ResponseBody
	public Result getDefaultValue() {
		Result result = new Result();
		DeliverySysParam deliverySysParam = deliverySysParamService.getByKey(DeliveryOrderDictTypeConstants.DELIVERY_DEFAULT_ADDRESS);
		String serviceName = "";
		String servicePhone = "";
		String serviceAddress = "";
		BigDecimal servicex = null;
		BigDecimal servicey = null;
		if (deliverySysParam != null) {
			String[] v = deliverySysParam.getValue().split(";");
			if (v.length == 5) {
				serviceName = v[0];
				servicePhone = v[1];
				serviceAddress = v[2];
				servicex = new BigDecimal(v[3]);
				servicey = new BigDecimal(v[4]);
			}
		}
		result.put("serviceName", serviceName);
		result.put("servicePhone", servicePhone);
		result.put("serviceAddress", serviceAddress);
		result.put("servicex", servicex);
		result.put("servicey", servicey);
		
		UserAddress userAddressQuery = new UserAddress();
		UserLoginVo userLoginVo = super.getLoginUser();
		Integer userId = userLoginVo.getId();
		userAddressQuery.setIsDefault(1);
		userAddressQuery.setCreatorId(userId);
		userAddressQuery.setStatus(1);
		Pageable pageKit = new PageKit(userAddressQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		List<UserAddress> userAddressList =  userAddressService.queryList(userAddressQuery, pageKit);
		if(userAddressList != null && userAddressList.size() > 0){
			result.put("userAddress", userAddressList.get(0));
		}
		return result;
	}
    
	@Override
	protected BaseService<DeliverySysParam> getBaseService() {
		return deliverySysParamService;
	}
}
